package heap

import "hi-jvm/classfile"

/*
字段信息
 */
type Field struct {
	ClassMember
	ConstValueIndex uint	// 如果属性为常量，则设置该常量在常量池中的索引
	SlotId          uint	// 属性编号
}

func newFields(class *Class, cfFields []*classfile.MemberInfo) []*Field {
	fields := make([]*Field, len(cfFields))
	for i, cfField := range cfFields {
		fields[i] = &Field{}
		fields[i].Class = class
		fields[i].copyMemberInfo(cfField)
		fields[i].copyAttributes(cfField)
	}
	return fields
}

func (self *Field) copyAttributes(cfField *classfile.MemberInfo) {
	valAttr := cfField.ConstantValueAttribute();
	if valAttr != nil {
		self.ConstValueIndex = uint(valAttr.ConstantValueIndex)
	}
}

func (self *Field) IsVolatile() bool {
	return isAccessFlagEqual(self.AccessFlags, ACC_VOLATILE)
}
func (self *Field) IsTransient() bool {
	return isAccessFlagEqual(self.AccessFlags, ACC_TRANSIENT)
}
func (self *Field) IsEnum() bool {
	return isAccessFlagEqual(self.AccessFlags, ACC_ENUM)
}

func (self *Field) isLongOrDouble() bool {
	return self.Descriptor == "J" || self.Descriptor == "D"
}

func (self *Field) isConst() bool {
	return self.IsStatic() && self.IsFinal()
}

func (self *Field) IsInt32() bool {
	switch self.Descriptor {
	case "Z", "B", "C", "S", "I":
		return true
	default:
		return false
	}
}

func (self *Field)IsRef() bool {
	switch self.Descriptor {
	case "L", "[":
		return true
	default:
		return false
	}
}

func (self *Field) IsInt64() bool {
	return self.Descriptor == "J"
}

func (self *Field) IsFloat() bool {
	return self.Descriptor == "F"
}

func (self *Field) IsDouble() bool {
	return self.Descriptor == "D"
}

func (self *Field)isString() bool {
	return self.Descriptor == "Ljava/lang/String;"
}

func (self *Field)isMine(field *FieldRef) bool {
	return  field.Name == field.Name && field.Descriptor == field.Descriptor
}
